﻿{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>风控白名单</title>
	
    <link rel="stylesheet" type="text/css" href=" {% static 'themes/default/easyui.css' %}">
    <link rel="stylesheet" type="text/css" href=" {% static 'themes/icon.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/portal.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/default/devbase.css' %}">
	<link rel="stylesheet" type="text/css" href=" {% static 'themes/default/base.css' %}">
	
	<script type="text/javascript" src=" {% static 'scripts/jquery.min.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/jquery.easyui.min.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/jquery.portal.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/easyui-lang-zh_CN.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/json2.js' %}"></script>
	<script type="text/javascript" src=" {% static 'scripts/devbase.js' %}"></script>
</head>
<body class="easyui-layout">
	<div data-options="region:'center'" class="easyui-layout">
		<!-- 查询面板 -->
		<div class="easyui-panel" 
		    style="width:100%;height:150px;padding:10px;"
			data-options="iconCls:'icon-search',closable:false,region:'north',title:'风控白名单'">

            <table>
				<tr>
					<td class="label">产品:</td>                                                                                                
					<td><select id="search_combo_prod_id" 
								class="easyui-combobox" 
								style="width:200px;" 
								data-options="valueField:'product_id',
											  textField:'product_name',
											  disabled:false">
						</select></td>
				</tr>
                <tr>
                    
                    <td class="label">证券代码:</td>
                    <td><select 
								class="easyui-combobox" 
								style="width:200px;" 
								data-options="valueField:'invest_account_id',
												textField:'invest_account_name',
												disabled:false,
												editable:true">
						</select></td>
						
					<td class="label">投顾账号 :</td>                                                                                   <!-- 下拉框显示 改为 account_name -->
                    <td><select id="search_account_id" 
								class="easyui-combobox" 
								style="width:200px;" 
								data-options="valueField:'invest_account_id',
												textField:'invest_account_name',
												disabled:false,
												editable:true">
						</select></td>
						
                    <td><a id="search_btn" href="#" 
							class="easyui-linkbutton" 
							data-options="iconCls:'icon-search'">查询</a>
					</td>
                    <form method="post" id="file_form" enctype="multipart/form-data" >
                        <td><a id="export_btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">导出Excel</a></td>
                        <td><P id="MyFile"><input type="file" name="fileImport" id="fileImport"  style="width:330px;" onchange="fileSelected();" /></P></td>
                        <td><a id="upload_btn" href="#" class="easyui-linkbutton" data-options="">上传</a></td>
                        <td><a id="download_btn" href="#" class="easyui-linkbutton" data-options="">下载</a></td>
                    </form>
                </tr>
				
			</table>

		</div>

		<!-- 查询表格 -->
		<div class="easyui-panel"  data-options="closable:false,region:'center',title:''">
			<table class="easyui-datagrid" 
					title="白名单查询" 
					style="width:100%;height:100%;"
                    id='dg'
					data-options="rownumbers:true,
								singleSelect:true,
								collapsible:true,
								toolbar:toolbar,
								method:'get'">
				<thead>
					<tr>
						<th data-options="field:'rule_id',width:80" style="display:none">ID</th>
						<th data-options="field:'invest_account_id',width:120" style="display:none">投顾id</th>
						<th data-options="field:'invest_account_name',width:120">投顾账号</th>
						<th data-options="field:'stock_code',width:120">证券代码</th>
						<th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>
					</tr>
				</thead>
			</table>
		</div>
	
	</div>
	
	<!-- 操作对话框 -->
	<div id="dlg_add" class="easyui-dialog" title="编辑"
		style="width:400px;height:300px;padding:10px;"
		data-options="
				iconCls: 'icon-save',
				toolbar: '',
				buttons: [{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
					    submitForm();
					}
				},{
					text:'关闭',
					handler:function(){
					    closeForm();
					}
				}]
			">
        <div style="margin:20px 20px 0;">
            <form id="ff" method="post">
                <table cellpadding="5">
                    <tr>
                        <td class="label">投顾账号:</td>
                        <td><select id="account_id" name="formobj.account_id"
                                    class="easyui-combobox"
                                    style="width:200px;"
                                    data-options="valueField:'invest_account_id',
                                                    textField:'invest_account_name',
                                                    disabled:false,
                                                    editable:true">
                        </select></td>
                    </tr>

                    <tr>
                        <td class="label">证券代码:</td>
                        <td><select id="stock_code" name="formobj.stock_code"
                                    class="easyui-combobox"
                                    style="width:200px;"
                                    data-options="valueField:'invest_account_id',
                                                    textField:'invest_account_name',
                                                    disabled:false,
                                                    editable:true">
                        </select></td>
                    </tr>

                </table>
                <input type="hidden" name="formobj.rule_id" id="rule_id" />
            </form>
        </div>
	</div>

	<script type="text/javascript">
		// 表格工具栏
		var toolbar = [ {
			text : '新增',
			iconCls : 'icon-add',
			handler : function() {
				$('#dlg_add').dialog('open');;
			}
		},  {
			text : '删除',
			iconCls : 'icon-remove',
			handler : function() {
				var selected = getSelected();

				if (selected == "") {
					alert("请选择要删除的记录");
					return;
				} else {
					deleteData(selected);
				}
			}
		} ];

		function getSelected() {
			var selected = "";
			var row = $('#dg').datagrid('getSelected');
			if (row) {
				selected = row.rule_id;
			}
			return selected;
		}

	    function deleteData(rule_id) {
			$.post('/rm/cmRiskWhite/delete?rule_id=' + rule_id, function(data) {
					if (data) {
						//alert("操作成功");
						//window.location.reload();

					}
				}
			);

			setTimeout(queryData, 500);
		}

		function submitForm() {
			// alert('submitForm');
			var formParam = $("#ff").serialize();//序列化表格内容为字符串

			$.get('/rm/cmRiskWhite/save?' + formParam, function (data) {
                console.log(data);
            });

            //setInterval(queryData, 500);
            setTimeout(queryData, 500);
            $('#dlg_add').dialog('close');
		}
		
		function closeForm() {
			$('#dlg_add').dialog('close');
		}
		
		function formatOper(val, row, index) {
			return '<a href="#" onclick="editData(' + index + ')">修改</a>';

		}

		function editData(index) {
			$('#dg').datagrid('selectRow', index);// 关键在这里
			var row = $('#dg').datagrid('getSelected');
			if (row) {
				$('#dlg_add').dialog('open');
				loadFF(row.rule_id);
			}
		}

		function loadFF(rule_id) {

		    $.get('/rm/cmRiskWhite/query?rule_id=' + rule_id, function (data) {
                console.log(data);
                obj = JSON.parse(data)
                // 使用combobox
                $("#account_id").combobox('setValue', obj.rows[0].invest_account_id);
                // 使用 combogrid
                $("#stock_code").combobox('setValue', obj.rows[0].stock_code);

                $("#rule_id").val(obj.rows[0].rule_id);
            });
		}

		function queryData() {

		    $.get('/rm/cmRiskWhite/query', function (data) {
                console.log(data);
                obj = JSON.parse(data)
                $('#dg').datagrid('loadData', obj);
            });

		}

        //查询按钮操作
        $('#search_btn').bind('click', queryData);

	    //导出excel
        $('#export_btn').bind('click', function (){
		    //alert("export.");
            // 提交表单
            $('#file_form').form('submit', {
                url:"/rm/cmRiskWhite/export_excel",
                success : function(data) {
                //alert("download");
                }
            });
		});
		//上传文件
		$('#upload_btn').bind('click', function (){
		    //alert("upload_btn.");
		    $('#file_form').form('submit', {
                url:"/rm/cmRiskWhite/file_upload",
                beforeSend: function(xhr, settings){
                    var csrftoken = $.cookie('csrftoken');
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                },
                success : function(data) {
                }
            });
		});
		//下载文件
		$('#download_btn').bind('click', function (){
		    //alert("download_btn.");
			// 提交表单
            $('#file_form').form('submit', {
                url:"/rm/cmRiskWhite/file_download",
                success : function(data) {
                //alert("download");
                }
            });
		});
		// 初始化
		$(function() {
			// 隐藏
			$('#dlg_add').dialog('close');

            $.get('/rm/cmRiskWhite/init', function (data) {
                console.log(data);
                obj = JSON.parse(data)
                obj[0]['selected'] = true
                $('#search_combo_prod_id').combobox('loadData', obj);
            });

            queryData();
		});
	
	</script>
	
</body>
</html>
